package com.zf.generator.wizards;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.ui.INewWizard;
import org.eclipse.ui.IWorkbench;
import com.zf.generator.Activator;
import com.zf.generator.ProjectBean;
import com.zf.generator.Tools;
import com.zf.generator.bean.JavaTemplateArgs;
import com.zf.generator.bean.JspTemplateArgs;
import com.zf.generator.templates.ActionTemplate;
import com.zf.generator.templates.DaoTemplate;
import com.zf.generator.templates.JspInputTemplate;
import com.zf.generator.templates.JspListTemplate;
import com.zf.generator.templates.ServiceTemplate;

public class CodeGenerationWizard extends Wizard implements INewWizard {

	private CodeGenerationWizardPage page;
	private ISelection selection;
	private ProjectBean projectVo;
	// java代码生成
	private List<JavaTemplateArgs> javaTemplateArgsList;
	// jsp代码生成
	private List<JspTemplateArgs> jspTemplateArgsList;

	public CodeGenerationWizard() {
		super();
		setNeedsProgressMonitor(true);
		setDefaultPageImageDescriptor(Activator
				.getImageDescriptor("icons/wizard.jpg"));
		setWindowTitle("代码生成");
	}

	@Override
	public void addPages() {
		page = new CodeGenerationWizardPage(selection);
		addPage(page);
	}

	@Override
	public boolean performFinish() {
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor)
					throws InvocationTargetException {
				monitor.beginTask("代码生成", 100);

				// 定义模板
				DaoTemplate daoTemplate = new DaoTemplate();
				ServiceTemplate serviceTemplate = new ServiceTemplate();
				ActionTemplate actionTemplate = new ActionTemplate();
				JspListTemplate jspListTemplate = new JspListTemplate();
				JspInputTemplate jspInputTemplate = new JspInputTemplate();

				// -------------------------------------java代码生成-------------------------------------

				monitor.worked(1);
				monitor.subTask("java代码生成");
				for (JavaTemplateArgs javaTemplateArgs : javaTemplateArgsList) {

					try {
						// 创建包
						IPackageFragmentRoot packageFragmentRoot = projectVo
								.getJavaProject().findPackageFragmentRoot(
										new Path("/" + projectVo.getName()
												+ "/src"));
						IPackageFragment packageFragment = packageFragmentRoot
								.getPackageFragment(javaTemplateArgs
										.getPackageName());
						if (!packageFragment.exists()) {
							packageFragment = packageFragmentRoot
									.createPackageFragment(
											javaTemplateArgs.getPackageName(),
											true, null);
						}

						// 生成java代码
						String javaCode = "";
						switch (javaTemplateArgs.getType()) {
						case DAO:
							javaCode = daoTemplate.generate(javaTemplateArgs);
							break;
						case SERVICE:
							javaCode = serviceTemplate
									.generate(javaTemplateArgs);
							break;
						case ACTION:
							javaCode = actionTemplate
									.generate(javaTemplateArgs);
							break;
						}
						packageFragment.createCompilationUnit(
								javaTemplateArgs.getClassName() + ".java",
								javaCode, true, new NullProgressMonitor());
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
				// -------------------------------------jsp代码生成-------------------------------------
				monitor.worked(1);
				monitor.subTask("jsp代码生成");
				for (JspTemplateArgs jspTemplateArgs : jspTemplateArgsList) {
					// 创建文件夹
					String directoryPath = projectVo.getWebInf()
							+ "/jsp/"
							+ jspTemplateArgs.getModeName()
							+ "/"
							+ jspTemplateArgs.getClassNameInfo()
									.getEntityObjectName();
					Tools.createDirectory(directoryPath);
					// 创建文件
					String filePath = directoryPath + "/"
							+ jspTemplateArgs.getJspName();
					Tools.createFile(filePath);

					// 写文件
					String jspCode = "";
					switch (jspTemplateArgs.getType()) {
					case LIST:
						jspCode = jspListTemplate.generate(jspTemplateArgs);
						break;
					case INPUT:
						jspCode = jspInputTemplate.generate(jspTemplateArgs);
						break;
					default:
						break;
					}
					Tools.writeStringToFile(filePath, jspCode);
				}
				monitor.done();
			}
		};

		try {
			getContainer().run(true, false, op);
		} catch (InvocationTargetException e) {
			e.printStackTrace();
			return false;
		} catch (InterruptedException e) {
			e.printStackTrace();
			return false;
		}

		/**
		 * 刷新工程
		 */
		projectVo.refresh();

		return true;
	}

	@Override
	public void init(IWorkbench workbench, IStructuredSelection selection) {
		this.selection = selection;
		projectVo = new ProjectBean(selection);
		try {
			javaTemplateArgsList = Tools.createJavaTemplateArgsList(selection);
			jspTemplateArgsList = Tools.createJspTemplateArgsList(selection);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}